﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThridGroup.ERP.Application.Contracts.DTO;
using ThridGroup.ERP.Application.Contracts.ProductionSystem;
using ThridGroup.ERP.ProductionSystem;
using Volo.Abp.AspNetCore.Mvc;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 点检保养项目控制器   ---tsh
    /// </summary>
    [Area("ProductionSystem")]
    public class MaintenanceplanController : AbpController
    {
        private readonly IMaintenanceplanServcices _maintenanceplanServices;

        public MaintenanceplanController(IMaintenanceplanServcices maintenanceplanServices)
        {
            _maintenanceplanServices = maintenanceplanServices;
        }

        #region 视图方法

        /// <summary>
        /// 主页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View("~/Views/ProductionSystem/Maintenanceplan/Index.cshtml");
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View("~/Views/ProductionSystem/Maintenanceplan/Add.cshtml");
        }

        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="id">项目ID</param>
        /// <returns></returns>
        public IActionResult Edit(int id)
        {
            ViewBag.Id = id;
            return View("~/Views/ProductionSystem/Maintenanceplan/Edit.cshtml");
        }

        #endregion

        #region API方法

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetList([FromQuery] QueryMaintenanceplanDto input)
        {
            try
            {
                var result = await _maintenanceplanServices.GetListAsync(input);
                return Json(new
                {
                    code = 0,
                    msg = "",
                    count = result.TotalCount,
                    data = result.Items
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    code = 500,
                    msg = ex.Message,
                    count = 0,
                    data = new List<MaintenanceplanDto>()
                });
            }
        }

        /// <summary>
        /// 根据ID获取详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int id)
        {
            try
            {
                var result = await _maintenanceplanServices.GetAsync(id);
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "获取成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 创建新项目
        /// </summary>
        /// <param name="input">创建数据</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CreateUpdateMaintenanceplanDto input)
        {
            try
            {
                var result = await _maintenanceplanServices.CreateAsync(input);
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "创建成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 更新项目
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">更新数据</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update(int id, [FromBody] CreateUpdateMaintenanceplanDto input)
        {
            try
            {
                var result = await _maintenanceplanServices.UpdateAsync(id, input);
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "更新成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                await _maintenanceplanServices.DeleteAsync(id);
                return Json(new
                {
                    success = true,
                    message = "删除成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 批量删除项目
        /// </summary>
        /// <param name="ids">主键ID列表</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteBatch([FromBody] List<int> ids)
        {
            try
            {
                await _maintenanceplanServices.DeleteBatchAsync(ids);
                return Json(new
                {
                    success = true,
                    message = $"成功删除 {ids.Count} 条记录"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 生成项目编码
        /// </summary>
        /// <param name="projectType">项目类型</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GenerateProjectCode(string projectType)
        {
            try
            {
                var result = await _maintenanceplanServices.GenerateProjectCodeAsync(projectType);
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "生成成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 检查项目编码是否存在
        /// </summary>
        /// <param name="projectCode">项目编码</param>
        /// <param name="excludeId">排除的ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> IsProjectCodeExist(string projectCode, int? excludeId = null)
        {
            try
            {
                var result = await _maintenanceplanServices.IsProjectCodeExistAsync(projectCode, excludeId);
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "检查完成"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取所有项目类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProjectTypes()
        {
            try
            {
                var result = await _maintenanceplanServices.GetProjectTypesAsync();
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "获取成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }

        #endregion
    }
}
